package com.powermanager.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.powermanager.R;
import com.powermanager.util.SPUtil;

import java.io.IOException;
import java.net.Socket;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SetIPActivity extends AppCompatActivity {


    @Bind(R.id.set_server_ip)
    EditText setServerIp;
    @Bind(R.id.set_server_sure)
    Button setServerSure;

    public Handler mHander = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 0:
                    Toast.makeText(SetIPActivity.this,"连接成功",Toast.LENGTH_SHORT).show();
                    break;
                case 1:
                    Toast.makeText(SetIPActivity.this,"连接失败",Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set_ip);
        ButterKnife.bind(this);

        String ipAndPort =  SPUtil.getSharedStringData(SetIPActivity.this,"ipAddress","");
        if("".equals(ipAndPort)){
            setServerIp.setText("");
        }else{
            ipAndPort = ipAndPort + ":" + SPUtil.getSharedIntData(SetIPActivity.this,"port",0);
            setServerIp.setText(ipAndPort);
        }


    }

    @OnClick(R.id.set_server_sure)
    public void onClick() {
        String ip = setServerIp.getText().toString();
        if(ip==null || "".equals(ip)){
            Toast.makeText(SetIPActivity.this,"IP地址和端口号不能为空！",Toast.LENGTH_SHORT).show();
            return;
        }

        String[] tmp = ip.split(":");

        SPUtil.setSharedStringData(SetIPActivity.this,"ipAddress",tmp[0]);
        SPUtil.setSharedIntData(SetIPActivity.this,"port",Integer.parseInt(tmp[1]));

        new Thread(){
            @Override
            public void run() {
                Socket client = null;
                try {
                    client = new Socket(SPUtil.getSharedStringData(SetIPActivity.this,"ipAddress"),SPUtil.getSharedIntData(SetIPActivity.this,"port"));
                    client.setSoTimeout(10000);
                    if(client.isConnected()){
                        mHander.sendEmptyMessage(0);
                        client.close();
                        finish();
                    }else {
                        mHander.sendEmptyMessage(1);
                    }

                } catch (IOException e) {
                    e.printStackTrace();
                    mHander.sendEmptyMessage(1);
                }
            }
        }.start();






    }

    @Override
    public void onBackPressed() {
    }
}
